package cc.bukkitPlugin.banitem.manager;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;

import cc.bukkitPlugin.banitem.BanItem;
import cc.bukkitPlugin.banitem.CFG;
import cc.bukkitPlugin.util.config.CommentedSection;
import cc.bukkitPlugin.util.config.CommentedValue;
import cc.bukkitPlugin.util.config.CommentedYamlConfig;
import cc.bukkitPlugin.util.plugin.ABukkitPlugin;
import cc.bukkitPlugin.util.plugin.fileManager.AConfigManager;

public class ConfigManager extends AConfigManager<BanItem>{

    private boolean mSetNull=true;
    private boolean mRemoveBanedItemFromNBT=false;
    private boolean mRemoveNoUseItemFromNBT=false;
    private boolean mCheckItemId=false;
    private boolean mCheckItemDamage=false;
    private boolean mCheckBlockAroundWhenPlace=false;

    public ConfigManager(BanItem pPlugin){
        super(pPlugin,"1.4");
    }

    @Override
    public void addDefaults(){
        super.addDefaults();

        if(this.mConfig.contains("ChatPrefix")){
            this.mConfig.set("MsgPrefix",this.mConfig.removeCommentedValue("ChatPrefix"));
        }
        this.mConfig.set("Color.Chat",null);

        String comment="BanItem世界物品不继承列表\n格式和例子: \nWorldItemListNotInherit\n  Main:\n  - BanList\n以上的例子就是说,Main世界的Ban列表不继承默认的列表,需要读取自 BanItem/WorldItem/Main/BanList.yml\n默认列表为 插件配置文件夹下的三个文件\n每个世界自己的列表在 BanItem/WorldItem/世界名/下\n所有的列表类型:\nBanList       物品黑名单\nNBTCheckList  NBT检查列表\nNoUseList     物品禁止使用列表";
        this.mConfig.addDefaultComments("WorldItemListNotInherit",comment.split("\n"));

        this.mConfig.addDefault("BanItem.Confiscate",true,"是否没收黑名单物品");
        this.mConfig.addDefaultComments("BanItem","BanItem插件主配置");
        this.mConfig.addDefault("BanItem.CheckInvWhenPlayerJoin",true,"当玩家登录的时候,检查登录的玩家背包里是否有黑名单物品","强制回收或没收请至少启用一项,否则检查将没有任何意义");
        this.mConfig.addDefault("BanItem.CheckInvDelayTicks",50,"玩家登陆多少Ticks后检查背包","请不要调的太小,太小可能会在没收东西是造成客户端崩溃");
        this.mConfig.addDefault("BanItem.CheckWhenPlayerSwitchItem",true,"是否在玩家切换手中物品的时候检查黑名单列表");
        this.mConfig.addDefault("BanItem.CheckBanItemInBlockNBT",true,"是否在方块的NBT中搜索黑名单物品");
        this.mConfig.addDefault("BanItem.CheckBlockAroundWhenPlace",false,"是否在玩家放置方块时检查方块周围是否有包含黑名单物品的方块");
        this.mConfig.addDefault("BanItem.ForceConfiscateWhenCheckInv",true,"检查背包时,强制回收背包里的黑名单物品,用于回收功能性饰品");
        this.mConfig.addDefault("BanItem.RemoveBanedItemFromNBT",false,"以下两项不建议开启,但是如果你服务器跑着没啥事开着也没事","从NBT中移除黑名单物品");
        this.mConfig.addDefault("BanItem.RemoveNoUseItemFromNBT",false,"从NBT中移除被禁用的物品");
        this.mConfig.addDefaultComments("Permission","权限节点配置");
        this.mConfig.addDefault("Permission.CheckItemId",false,"是否启用物品ID权限支持","未启用之前只有BanItem.bypass.ban这样是最细的权限","启用之后可以更细分到BanItem.bypass.ban.54的权限");
        this.mConfig.addDefault("Permission.CheckItemDamage",false,"是否启用物品子ID权限支持,必须先启用CheckItemId","启用之后可以更细分到BanItem.bypass.ban.54@1的权限");
        this.mConfig.addDefaultComments("Color","聊天窗口输出字体颜色设置","此配置可以活用,其实这就是消息的前缀");
        this.mConfig.addDefault("Color.Link",CFG.link_color,"带功能的文字颜色");
        this.mConfig.addDefault("Color.Help",CFG.help_color,"命令帮助字体颜色");
        this.mConfig.addDefault("Color.Command",CFG.cmd_color,"命令类字体颜色");
        this.mConfig.addDefault("Color.CfgWord",CFG.cfg_color,"配置类字体颜色");
    }

    @Override
    public boolean reloadConfig(CommandSender pSender){
        if(!super.reloadConfig(pSender)){
            ABukkitPlugin.severe(pSender,C("MsgErrorHappendWhenReloadConfig"));
            return false;
        }
        this.checkUpdate();
        this.addDefaults();

        this.mSetNull=this.mConfig.getBoolean("BanItem.Confiscate");
        this.mRemoveBanedItemFromNBT=this.mConfig.getBoolean("BanItem.RemoveBanedItemFromNBT");
        this.mRemoveNoUseItemFromNBT=this.mConfig.getBoolean("BanItem.RemoveNoUseItemFromNBT");
        this.mCheckItemId=this.mConfig.getBoolean("Permission.CheckItemId");
        this.mCheckItemDamage=this.mConfig.getBoolean("Permission.CheckItemDamage");
        this.mCheckBlockAroundWhenPlace=this.mConfig.getBoolean("BanItem.CheckBlockAroundWhenPlace");

        CFG.link_color=this.mConfig.getString("Color.Link",CFG.link_color);
        CFG.help_color=this.mConfig.getString("Color.Help",CFG.help_color);
        CFG.cmd_color=this.mConfig.getString("Color.Command",CFG.cmd_color);
        CFG.cfg_color=this.mConfig.getString("Color.CfgWord",CFG.cfg_color);

        ABukkitPlugin.info(pSender,C("MsgConfigReloaded"));

        ABukkitPlugin.info(pSender,">从NBT中移除黑名单物品: "+this.mRemoveBanedItemFromNBT);
        ABukkitPlugin.info(pSender,">从NBT中移除禁用物品: "+this.mRemoveNoUseItemFromNBT);
        ABukkitPlugin.info(pSender,">没收黑名单物品: "+this.mSetNull);
        ABukkitPlugin.info(pSender,">放置方块时检查周围方块: "+this.mCheckBlockAroundWhenPlace);

        return this.saveConfig(null);
    }

    @Override
    protected boolean checkUpdate(){
        String tVersion=this.mConfig.getString("version","1.0");
        if(tVersion.compareToIgnoreCase("1.0")<=0){ // 1.0-1.1
            tVersion="1.1";
            String[] paths=new String[]{"BanItem.BanList","BanItem.NoUseList"};
            for(String sPath : paths){
                CommentedSection listSec=this.mConfig.getConfigurationSection(sPath);
                if(listSec==null)
                    listSec=this.mConfig.createSection(sPath);
                for(String sKeyItem : listSec.getKeys(false)){
                    CommentedSection itemSec=listSec.getConfigurationSection(sKeyItem);
                    if(itemSec==null)
                        continue;
                    for(String sKeyDamage : itemSec.getKeys(false)){
                        CommentedSection damageSec=itemSec.getConfigurationSection(sKeyDamage);
                        if(damageSec==null)
                            continue;
                        Map<String,Object> nbtValue=new HashMap<String,Object>();
                        for(String sNBTkey : damageSec.getKeys(false))
                            nbtValue.put(sNBTkey,damageSec.removeCommentedValue(sNBTkey));
                        damageSec.set("reason","");
                        damageSec.createSection("nbt",nbtValue);
                    }
                }
            }
            ABukkitPlugin.info("配置文件已经从1.0升级到1.1");
        }
        if(tVersion.compareToIgnoreCase("1.1")<=0){// 1.1-1.2
            tVersion="1.3";
            CommentedYamlConfig tConfig=new CommentedYamlConfig();
            File opFile=null;
            String[] paths=new String[]{"BanList","NoUseList"};
            for(String sFile : paths){
                opFile=new File(this.mPlugin.getDataFolder(),sFile+".yml");
                tConfig.loadFromFile(opFile);
                ConfigurationSection tSec=this.mConfig.getConfigurationSection("BanItem."+sFile);
                this.mConfig.set("BanItem."+sFile,null);
                if(tSec==null)
                    continue;
                for(String sNodekey : tSec.getKeys(false))
                    tConfig.set(sNodekey,tSec.get(sNodekey));
                tConfig.saveToFile(opFile);
            }
            opFile=new File(this.mPlugin.getDataFolder(),"NBTCheckList.yml");// NBTCheckList
            tConfig.loadFromFile(opFile);
            ItemManager importMan=new ItemManager((BanItem)this.mPlugin);
            this.mPlugin.unregisterConfigModel(importMan);
            CommentedYamlConfig saveConfig=new CommentedYamlConfig();
            importMan.saveItemToConfig(saveConfig,importMan.getSimpleItem(this.mConfig.getStringList("BanItem.NBTCheckList")));
            saveConfig.saveToFile(new File(this.mPlugin.getDataFolder(),"NBTCheckList.yml"));
            this.mConfig.set("BanItem.NBTCheckList",null);
        }
        if(tVersion.compareToIgnoreCase("1.3")<=0){
            tVersion="1.4";
            CommentedValue tValue=this.mConfig.removeCommentedValue("WordItemListNotInherit");
            if(tValue!=null){
                this.mConfig.set("WorldItemListNotInherit",tValue);
            }
        }
        this.mConfig.set(SEC_CFG_VERSION,mVersion);
        return true;
    }

    public boolean isSetNull(){
        return this.mSetNull;
    }

    public boolean isRemoveBanedItemFromNBT(){
        return this.mRemoveBanedItemFromNBT;
    }

    public boolean isRemoveNoUseItemFromNBT(){
        return this.mRemoveNoUseItemFromNBT;
    }

    public boolean isCheckBlockAroundWhenPlace(){
        return this.mCheckBlockAroundWhenPlace;
    }

    public boolean isCheckItemId(){
        return this.mCheckItemId;
    }

    public boolean isCheckItemDamage(){
        return this.mCheckItemDamage;
    }

}
